package com.woniuxy.movie.cinema.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.movie.cinema.dao.CinemaHallMapper;
import com.woniuxy.movie.cinema.dao.CinemaSeatMapper;
import com.woniuxy.movie.cinema.exception.CinemaException;
import com.woniuxy.movie.cinema.form.cinemahall.HallAddForm;
import com.woniuxy.movie.cinema.form.cinemahall.SelectHallForm;
import com.woniuxy.movie.cinema.form.updateStateForm;
import com.woniuxy.movie.cinema.model.CinemaHall;
import com.woniuxy.movie.cinema.model.CinemaSeat;
import com.woniuxy.movie.cinema.service.CinemaHallService;
import com.woniuxy.movie.cinema.service.CinemaSeatService;
import com.woniuxy.movie.commons.web.util.StateData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 影厅详情信息 服务实现类
 * </p>
 *
 * @author 刘存益
 * @since 2023年11月17日
 */
@Service
public class CinemaHallServiceImpl extends ServiceImpl<CinemaHallMapper, CinemaHall> implements CinemaHallService {


    @Autowired
    private CinemaHallMapper cinemaHallMapper;
    @Autowired
    private CinemaSeatMapper cinemaSeatMapper;
    @Autowired
    private CinemaSeatService cinemaSeatService;

    /**
     * 新增影厅
     * @param form
     */
    @Override
    @Transactional
    public void add(HallAddForm form) {
        //判断影厅名是否重复
        if (cinemaHallMapper.exists(new LambdaQueryWrapper<CinemaHall>().eq(CinemaHall::getHallName,form.getHallName()))) {
            throw new CinemaException(3001,"影厅名重复");
        }
        //新增影院
        CinemaHall cinemaHall = BeanUtil.toBean(form, CinemaHall.class);
        cinemaHall.setHallState(StateData.HALL_STATE_NORMAL);
        cinemaHallMapper.insert(cinemaHall);
        //添加座位
        ArrayList<CinemaSeat> cinemaSeats = new ArrayList<>();
        int seatRow = 1;
        int seatCol = 1;
        for (int i = 0; i < form.getHallMax(); i++) {
            CinemaSeat cinemaSeat = new CinemaSeat();
            cinemaSeat.setSeatRow(seatRow);
            cinemaSeat.setSeatCol(seatCol);
            cinemaSeat.setHallId(cinemaHall.getHallId());
            cinemaSeat.setSeatState(StateData.SEAT_STATE_NORMAL);
            cinemaSeats.add(cinemaSeat);
            if (seatCol==10) {
                seatRow++;
                seatCol=0;
            }
            seatCol++;
        }
        cinemaSeatService.saveBatch(cinemaSeats);
    }

    /**
     * 查询所有影厅
     * @param form
     * @return
     */
    @Override
    public List<CinemaHall> select(SelectHallForm form) {
        QueryWrapper<CinemaHall> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(form.getHallName())) {
            queryWrapper.likeRight("hall_name",form.getHallName());
        }
        if (form.getHallState()!=null) {
            queryWrapper.eq("hall_state",form.getHallState());
        }
        return cinemaHallMapper.selectList(queryWrapper);
    }

    /**
     * 修改影厅状态
     * @param form
     */
    @Override
    public void udpateState(updateStateForm form) {
        CinemaHall cinemaHall = cinemaHallMapper.selectById(form.getHallId());
        if (cinemaHall==null) {
            throw new CinemaException(3002,"影厅不存在");
        }
        if (cinemaHall.getHallState().equals(StateData.HALL_STATE_DELETED)) {
            throw new CinemaException(3003,"影厅已被删除，不能进行此操作");
        }
        CinemaHall hall = new CinemaHall();
        hall.setHallId(cinemaHall.getHallId());
        if (form.getHallState().equals(StateData.HALL_STATE_DISABLED)) {
            if (cinemaHall.getHallState().equals(StateData.HALL_STATE_DISABLED)) {
                throw new CinemaException(3004,"请勿重复禁用");
            }
            //查询是否排场
            //修改
            hall.setHallState(StateData.HALL_STATE_DISABLED);
            cinemaHallMapper.updateById(hall);
            LambdaUpdateWrapper<CinemaSeat> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(CinemaSeat::getSeatState,StateData.SEAT_STATE_DISABLED);
            updateWrapper.eq(CinemaSeat::getHallId,cinemaHall.getHallId());
            cinemaSeatMapper.update(null,updateWrapper);
        }else if (form.getHallState().equals(StateData.HALL_STATE_NORMAL)){
            if (cinemaHall.getHallState().equals(StateData.HALL_STATE_NORMAL)) {
                throw new CinemaException(3005,"请勿重复启用");
            }
            hall.setHallState(StateData.HALL_STATE_NORMAL);
            cinemaHallMapper.updateById(hall);
            LambdaUpdateWrapper<CinemaSeat> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(CinemaSeat::getSeatState,StateData.SEAT_STATE_NORMAL);
            updateWrapper.eq(CinemaSeat::getHallId,cinemaHall.getHallId());
            cinemaSeatMapper.update(null,updateWrapper);
        }else if (form.getHallState().equals(StateData.HALL_STATE_DELETED)){
            //查询是否排场
            //修改
            hall.setHallState(StateData.HALL_STATE_DELETED);
            cinemaHallMapper.updateById(hall);
            LambdaUpdateWrapper<CinemaSeat> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(CinemaSeat::getSeatState,StateData.SEAT_STATE_DISABLED);
            updateWrapper.eq(CinemaSeat::getHallId,cinemaHall.getHallId());
            cinemaSeatMapper.update(null,updateWrapper);
        }else {
            throw new CinemaException(3006,"数据不合法");
        }

    }


}
